package gov.tax.rti.base.controller;

import gov.tax.rti.base.security.service.impl.YhbsServiceImpl;
import gov.tax.rti.core.security.entity.DataTag;
import gov.tax.rti.core.security.entity.User;
import gov.tax.rti.core.security.service.SecurityService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;

@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
@Component("gdgs.base.securityInterceptor")
public class SecurityInterceptor {
	
	@Resource
	private SecurityService securityService;
	@Resource
	private YhbsServiceImpl yhbsService;

	@DataProvider
	public Collection<DataTag> getSwjgs(Map<String,Object> parameter) throws Exception {
		String parentId = (String)parameter.get("swjgPid");
		/*Map<String, Object> swjgCondition = new HashMap<String, Object>();
		swjgCondition.put("yxbz", parameter.get("yxbz"));
		swjgCondition.put("xybz", parameter.get("xybz"));
		swjgCondition.put("jcbz", parameter.get("jcBz"));
		swjgCondition.put("jglx", parameter.get("swjgLx"));
		swjgCondition.put("nsrswjgbz", parameter.get("nsrswjgBz"));*/
		Collection<DataTag> dataTags = securityService.getCurrentSecDataTagChildren(parentId);//CurrentQxSwjgs(swjgPid, swjgCondition);//swjgPid = null
		if(dataTags != null){
			dataTags = EntityUtils.toEntity(dataTags);
			for (DataTag dataTag : dataTags) {
				EntityUtils.setValue(dataTag, "selectable", true);
			}
		}
		return dataTags;
	}
	
	@DataProvider
	public Collection<DataTag> getSwjgsCascade(Map<String,Object> parameter) throws Exception {
		/*String swjgPid = (String)parameter.get("swjgPid");
		Map<String, Object> swjgCondition = new HashMap<String, Object>();
		swjgCondition.put("yxbz", parameter.get("yxbz"));
		swjgCondition.put("xybz", parameter.get("xybz"));
		swjgCondition.put("jcbz", parameter.get("jcBz"));
		swjgCondition.put("jglx", parameter.get("swjgLx"));
		swjgCondition.put("nsrswjgbz", parameter.get("nsrswjgBz"));*/
		Collection<DataTag> dataTags = securityService.getCurrentSecDataTagTree();//QxSwjgsCascade(swjgPid, swjgCondition);//swjgPid = null
		initDataTagsSelectable(dataTags);
		return dataTags;
	}
	
	protected void initDataTagsSelectable(Collection<DataTag> dataTags) throws Exception {
		if(dataTags != null){
			dataTags = EntityUtils.toEntity(dataTags);
			for (DataTag dataTag : dataTags) {
				initDataTagsSelectable(dataTag.getChildren());
				EntityUtils.setValue(dataTag, "selectable", true);
			}
		}
	}

	@DataProvider
	public Collection<User> getCzrys(Map<String,Object> parameter) {
		Boolean selectable = (Boolean)parameter.get("selectable");
		if(selectable !=null && !selectable){
			return null;
		}
		String sjbqDm = (String)parameter.get("swjgId");
		/*String ryXybz = (String)parameter.get("ryXybz");
		String ryYxbz = (String)parameter.get("ryYxbz");*/
		return yhbsService.getYhbsBySjbqDm(sjbqDm);
	}

	@DataProvider
	public void getNSr(Page<Map<String,Object>> page, String filterValue) throws Exception {
		page.setEntities(getNSr(page.getPageSize(), filterValue));
		page.setEntityCount(page.getPageSize());
	}
	
	@DataProvider
	public List<Map<String,Object>> getNSr(String filterValue) throws Exception {
		return getNSr(20, filterValue);
	}
	
	public List<Map<String,Object>> getNSr(int pageSize,String filterValue) throws Exception {
		if(StringUtils.isEmpty(filterValue)){
			return null;
		}
		return yhbsService.getNSr(pageSize, filterValue);
	}
	
}
